/*
 * wait.cpp
 *
 *  Created on: Jul 13, 2013
 *      Author: m
 */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "NewSemaphore.h"

using namespace ipc;

int main ( void )
{
    char mostrar[10240];
    //Semaphore sem;
    char option;

    Semaphore sem = Semaphore::get( "/tmp/mysem", 'a' );

    sprintf( mostrar,
            "WAIT: "
            "Enter 'w' to wait or 'q' to quit.\n" );
    write( fileno(stdout), mostrar, strlen(mostrar) );

    while( std::cin >> option )
    {
        if ( option == 'q' || option == 'Q' )
        {
            break;
        }

        if ( option == 'w' || option == 'W' )
        {
            sprintf( mostrar,
                    "WAIT: "
                    "waiting...\n" );
            write( fileno(stdout), mostrar, strlen(mostrar) );

            sem.wait();

            sprintf( mostrar,
                    "WAIT: "
                    "Signal received.\n" );
            write( fileno(stdout), mostrar, strlen(mostrar) );
        }
    }

    return 0;
}
